package com.gao.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gao.weblog.common.domain.dos.TagDO;

import java.time.LocalDate;
import java.util.List;
import java.util.Objects;

public interface TagMapper extends BaseMapper<TagDO> {

    default Page<TagDO> selectPageList(Long current, Long size, String tagName, LocalDate tagStartDate, LocalDate tagEndDate){
        Page<TagDO> page = new Page<>(current, size);
        LambdaQueryWrapper<TagDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Objects.nonNull(tagName), TagDO::getName, tagName)
                .ge(Objects.nonNull(tagStartDate), TagDO::getCreateTime, tagStartDate)
                .le(Objects.nonNull(tagEndDate), TagDO::getCreateTime, tagEndDate)
                .orderByDesc(TagDO::getCreateTime);

        return selectPage(page, wrapper);
    }

    default List<TagDO> selectByKey(String key){
        return selectList(Wrappers.<TagDO>lambdaQuery()
                .like(TagDO::getName, key)
                .orderByDesc(TagDO::getCreateTime));
    }

    default List<TagDO> selectByTagIds(List<Long> tagIds){
        LambdaQueryWrapper<TagDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TagDO::getId, tagIds);
        return selectList(wrapper);
    }


}
